package com.zx.www.volunteersystem.controller;

import com.zx.www.volunteersystem.mapper.ActivityTypeMapper;
import com.zx.www.volunteersystem.pojo.ActivityType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activity-types")
public class ActivityTypeController {
    @Autowired
    private ActivityTypeMapper activityTypeMapper;

    // 添加活动分类
    @PostMapping
    public boolean addActivityType(@RequestBody ActivityType activityType) {
        try {
            int result = activityTypeMapper.insert(activityType);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据ID查询活动分类
    @GetMapping("/{typeId}")
    public ActivityType getActivityTypeById(@PathVariable Integer typeId) {
        return activityTypeMapper.selectById(typeId);
    }

    // 根据分类名称查询活动分类
    @GetMapping("/name/{typeName}")
    public ActivityType getActivityTypeByName(@PathVariable String typeName) {
        return activityTypeMapper.selectByTypeName(typeName);
    }

    // 获取所有活动分类
    @GetMapping
    public List<ActivityType> getAllActivityTypes() {
        return activityTypeMapper.selectAll();
    }

    // 更新活动分类
    @PutMapping("/{typeId}")
    public boolean updateActivityType(@PathVariable Integer typeId, @RequestBody ActivityType activityType) {
        activityType.setType_id(typeId);
        try {
            int result = activityTypeMapper.update(activityType);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除活动分类
    @DeleteMapping("/{typeId}")
    public boolean deleteActivityType(@PathVariable Integer typeId) {
        try {
            int result = activityTypeMapper.deleteById(typeId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
